{{define "cms/permission_edit.html"}}
{{ template "header" . }}
<style>
    .custom-radio{
        width: 18px;
        height: 18px;
    }
</style>

<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        权限管理 / 菜单管理 / 编辑菜单
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="form-horizontal">
                        <input type="hidden" name="token" value="{{ .token }}">
                        <input type="hidden" name="id" value="{{ .obj.ID }}">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">选择父级菜单</label>
                            <div class="col-sm-4">
                                <select class="form-control m-b" name="pid" aria-label="">
                                    <option value="0">--请选择父级菜单--</option>
                                    {{ range .lists }}
                                    <option value="{{.ID}}" id="blank_{{.ID}}" {{ if eq $.obj.Pid .ID}}selected{{end}}>
                                        <script type="text/javascript">
                                            function blank() {
                                                var str = "";
                                                for (var i = 0; i < parseInt("{{.Level}}");i ++) {
                                                    str += "&nbsp;&nbsp;"
                                                }
                                                return str + "{{.Name}}"
                                            }
                                            document.getElementById("blank_{{.ID}}").innerHTML = blank();
                                        </script>
                                    </option>
                                    {{end}}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单名称：</label>
                            <div class="col-sm-3">
                                <input type="text" placeholder="" name="name" value="{{.obj.Name}}" class="form-control" required> <span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">路径：</label>
                            <div class="col-sm-3">
                                <input type="text" placeholder="" name="path" value="{{.obj.Path}}" class="form-control" required> <span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">展示：</label>
                            <div class="col-sm-3 radio i-checks" style="padding-left: 15px">
                                <label><input type="radio" {{ if eq .obj.IsShow 1}}checked{{end}} value="1" name="is_show" class="custom-radio"> <i></i>是</label>
                                <label style="margin-left: 15px"><input type="radio" {{ if eq .obj.IsShow 0}}checked{{end}} value="2" name="is_show" class="custom-radio"><i></i>否</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-3">
                                <button class="btn btn-sm btn-primary" onclick="edit_permission()">修 改</button>
                                <a href="/permission" class="btn btn-sm btn-white">取消</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

{{ template "footer" . }}
<script>
    function edit_permission(){
        var id = $("input[name='id']").val();
        var token = $("input[name='token']").val();
        var pid = $("select[name='pid']").val();
        var name = $("input[name='name']").val();
        var path = $("input[name='path']").val();
        var is_show = $("input[name='is_show']:checked").val();
        var prompt_msg = "";
        if(name === ""){
            prompt_msg = "菜单名称不能为空";
        }else if(is_show === ""){
            prompt_msg = "展示不能为空";
        }
        if (prompt_msg !== ""){
            layer.msg(prompt_msg, {icon: 2, time: 1200, title: "提示"});
            return;
        }

        $.post("/permissionedit", {id:id, name:name, pid:pid, path:path, show:is_show, token: token},function (res) {
            var jsonData = $.parseJSON(res);
            if(jsonData.status !== 200 && jsonData.status !== 20023){
                if(jsonData.status !== 20007) {
                    $("input[name='token']").val(jsonData.data);
                }
                layer.msg(jsonData.message, {icon: 2, time: 1200, title: "提示"});
            } else if(jsonData.status === 20023) {
                window.location.href = "/nopermission";
            } else {
                layer.msg(jsonData.message, {
                    icon: 1,
                    time: 1000,
                    title: "提示",
                    end:function () {
                        window.location.href = "/permission";
                    }
                });
            }
        });
    }
</script>
{{end}}